package com.xuyang.data;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.xuyang.utils.FoodMenu;

/**
 * Servlet implementation class SubmitOrder
 */
public class SubmitOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取桌位号
		int seatNumber=Integer.parseInt(request.getParameter("seatNumber"));
		//获取订单
		String submitOrder=request.getParameter("submitOrder");
		//将订单Json数据转化成可以识别的对象
		ArrayList<FoodMenu> arrayList=new ArrayList<FoodMenu>();
		JSONArray array=JSONArray.fromObject(submitOrder);
		arrayList=(ArrayList<FoodMenu>) JSONArray.toList(array, FoodMenu.class);
		System.out.println(seatNumber+"号桌已提交订单，请尽快处理！");
		System.out.println("****************************");
		System.out.println("菜名\t单价\t数量\t小计\t");
		int sum=0;
		for (FoodMenu food : arrayList) {
			sum=sum+food.getFoodCount()*food.getFoodPrice();
			System.out.println(food.getFoodName()+"\t"+food.getFoodPrice()+"\t"+food.getFoodCount()+"\t"+food.getFoodCount()*food.getFoodPrice());
		}
		System.out.println("总计:\t\t\t"+sum);
		System.out.println("****************************");
		response.getWriter().write("OK");
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		doGet(request, response);
	}
	
}
